import {
  Controller,
  Get,
  Post,
  Body,
  Patch,
  Param,
  Delete,
  UseFilters,
  UseGuards,
} from '@nestjs/common';
import { MenusService } from './menus.service';
import { CreateMenuDto } from './dto/create-menu.dto';
import { UpdateMenuDto } from './dto/update-menu.dto';
import { TypeormFilter } from 'src/filters/typeorm.filter';
import { CaslGuard } from 'src/guards/casl/casl.guard';
import { Can, Cannot, CheckPolicies } from 'src/decorators/casl.decarator';
import { Action } from 'src/enum/action.enum';
import { Menus } from './entities/menu.entity';
import { JwtGuard } from 'src/guards/admin/jwt.guard';
import { AdminGuard } from 'src/guards/admin/admin.guard';


@Controller('menus')
@UseFilters(new TypeormFilter())  // 注入 typeOrm出错的过滤器

@UseGuards( JwtGuard , AdminGuard , CaslGuard)
@CheckPolicies((ability) => ability.can(Action.Manage , Menus))
@Can(Action.Read , Menus)
// @Cannot(Action.Create , Menus)
export class MenusController {
    constructor(private readonly menusService: MenusService) {}

    @Post('/create')
    create(@Body() createMenuDto: CreateMenuDto) {
        return this.menusService.create(createMenuDto)
    }

    @Get('/all')
    @Can(Action.Update , Menus)
    findAll() {
        return this.menusService.findAll()
    }

    @Get(':id')
    findOne(@Param('id') id: string) {
        return this.menusService.findOne(id);
    }

    @Patch('/update/:id')
    update(@Param('id') id: string, @Body() updateMenuDto: Menus) {
        return this.menusService.update(id, updateMenuDto);
    }

    @Delete(':id')
    remove(@Param('id') id: string) {
        return this.menusService.remove(id);
    }
}
